//
//  FSCManagerController.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/29.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit

class FSCManagerController: UIViewController {

    fileprivate let btnW = screenWidth/2
    fileprivate let btnY = 0
    fileprivate let btnH = 44
    fileprivate var whiteLine:UIView!
    fileprivate let controllers = [UIViewController]()
    fileprivate var scrollView:UIScrollView!
    fileprivate var selectedBtn:UIButton!
    
    var hw_id:Int = 0

//MARK:视图生命周期
        
    override func viewWillAppear(_ animated: Bool) {
        view.tag = 1
        super.viewWillAppear(animated)
        tabBarController?.tabBar.isHidden = true
        let image = UIImage.imageWithColor(color: UIColor.colorWithHex(hexColor: blueColor))
        navigationController?.navigationBar.setBackgroundImage(image, for: .any, barMetrics: .default)
        
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.tintColor = UIColor.white
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.colorWithHex(hexColor: grayColor)
        setupUI()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        tabBarController?.tabBar.isHidden = false
    }
//MARK:设置UI

    func setupUI() -> Void {
        
        
        let btnName = ["客观题","主观题"]
        for item in 0..<2 {
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x: Int(btnW) * item, y: btnY  , width: Int(btnW), height: 44)
            btn.setTitle(btnName[item], for: .normal)
            btn.setTitleColor(UIColor.colorWithHex(hexColor: 0x005780), for: .normal)
            btn.setTitleColor(UIColor.white, for: .selected)
            btn.backgroundColor = UIColor.colorWithHex(hexColor: blueColor)
            btn.tag = btnBaseTAG + item
            btn.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
            view.addSubview(btn)
            if item == 0 {
                selectedBtn = btn
                selectedBtn.isSelected = true
            }
        }
        
        whiteLine = UIView(frame: CGRect(x: 0, y: btnH - 4, width: Int(screenWidth/2), height: 3))
        whiteLine.backgroundColor = UIColor.white
        view.addSubview(whiteLine)
        
        scrollView = UIScrollView()

        view.addSubview(scrollView)
        
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(btnH)
            make.left.equalTo(self.view)
            make.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
        }
        
        scrollView.contentSize = CGSize(width: screenWidth * 2, height: scrollView.bounds.height)
        scrollView.isPagingEnabled = true
        scrollView.bounces = true
        scrollView.delegate = self
        
        let objectiveItemVC = FSCObjectiveItemTableController()
        let subjectiveItemVC = FSCSubjectiveItemController()
        
        let vcArr = [objectiveItemVC,subjectiveItemVC]
        
        for (index,vc) in vcArr.enumerated() {
            addChildViewController(vc)
            vc.didMove(toParentViewController: self)
            
            vc.view.frame = CGRect(x: screenWidth * CGFloat(index), y: 0, width: screenWidth, height: scrollView.bounds.height)
            scrollView.addSubview(vc.view)
        }
    }
    
    
    //btn点击事件
    func btnClicked(sender:UIButton) -> Void {
        selectedBtn.isSelected = false
        selectedBtn = sender
        selectedBtn.isSelected = true
        
        UIView.animate(withDuration: 0.3) {
             self.whiteLine.frame.origin.x = screenWidth/2 * CGFloat(sender.tag - btnBaseTAG)
            self.scrollView.contentOffset = CGPoint(x: screenWidth*CGFloat(sender.tag-btnBaseTAG), y: 0)
        }
       
    }
}


extension FSCManagerController:UIScrollViewDelegate {
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let offsetX = scrollView.contentOffset.x
        let tag = Int(offsetX/screenWidth)
        
        let btn = view.viewWithTag(tag+btnBaseTAG) as? UIButton
        btnClicked(sender: btn!)
    }
}
